package org.yufan.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yufan.bean.User;
import org.yufan.common.Result;
import org.yufan.common.ResultUtils;
import org.yufan.exception.CustomerException;
import org.yufan.service.UserService;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/sso")
@Controller
public class UserController {


    @Autowired
    private UserService userService;

    private Logger LOGGER=LoggerFactory.getLogger( UserController.class);


    /**
     *
     * @param user
     * @param bindingResult
     * @return
     * @throws CustomerException
     */
    @RequestMapping(value = "/register",method =RequestMethod.POST)
    @ResponseBody
    public Result register(@Valid  User user, BindingResult bindingResult) throws CustomerException {
        //判断是否有错误
        if(bindingResult.hasErrors()){
            //校验失败
            //获取校验信息
            List<ObjectError> errors = bindingResult.getAllErrors();
            List<String> list= new ArrayList<>();
            for (ObjectError error:errors) {
                //获取每一个错误信息
                String msg=error.getDefaultMessage();
                list.add(msg);
            }
            //拼接字符串
            String message=org.apache.commons.lang3.StringUtils.join(list,",");
            LOGGER.info("校验失败，信息为{}",message);
            return ResultUtils.buildFail(105,message);
        }
        userService.register(user);
        return ResultUtils.buildSuccess();
    }


    /**
     *
     * @param username
     * @param password
     * @return
     * @throws CustomerException
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseEntity<Result> login(String username,String password) throws CustomerException {
        if(StringUtils.isEmpty(username)){
            LOGGER.info("用户名为空!");
            return ResponseEntity.ok(ResultUtils.buildFail(110,"用户名不能为空!"));
        }
        if(StringUtils.isEmpty(password)){
            LOGGER.info("密码为空!");
            return ResponseEntity.ok(ResultUtils.buildFail(111,"密码不能为空!"));
        }
        String token=userService.login(username,password);
        if(StringUtils.isEmpty(token)){
            LOGGER.info("用户{},登录失败",username);
            return  ResponseEntity.ok(ResultUtils.buildFail(112,"登录失败!"));
        }
        LOGGER.info("登录成功，当前登录的用户为{}",username);
        return ResponseEntity.ok(ResultUtils.buildSuccess(token));
    }


    /**
     *
     * @return
     * @throws CustomerException
     */
    @RequestMapping(value = "/loginByPhone",method = RequestMethod.POST)
    public ResponseEntity<Result> loginByPhone(String phone,String code) throws CustomerException {
        if(StringUtils.isEmpty(phone)){
            LOGGER.info("手机号码!");
            return ResponseEntity.ok(ResultUtils.buildFail(110,"手机号码为空!"));
        }
        if(StringUtils.isEmpty(code)){
            LOGGER.info("验证码为空!");
            return ResponseEntity.ok(ResultUtils.buildFail(111,"验证码不能为空!"));
        }
        String token=userService.loginByPhone(phone,code);
        if(StringUtils.isEmpty(token)){
            LOGGER.info("用户{},登录失败",phone);
            return  ResponseEntity.ok(ResultUtils.buildFail(112,"登录失败!"));
        }
        LOGGER.info("登录成功，当前登录的用户为{}",phone);
        return ResponseEntity.ok(ResultUtils.buildSuccess(token));
    }



    /**
     *
     * @param token
     * @return
     * @throws CustomerException
     */
    @RequestMapping(value = "/query/{token}",method = RequestMethod.POST)
    public ResponseEntity<Result> queryUserByToken(@PathVariable("token")  String token) throws CustomerException {
        LOGGER.info("当前查询用户信息的token为{}",token);
        User user=userService.queryUserByToken(token);
        if(user==null){
            return  ResponseEntity.ok(ResultUtils.buildFail(113,"登录失效!"));
        }
        return ResponseEntity.ok(ResultUtils.buildSuccess(user));
    }



}
